<?php
namespace Admin\Controller;
use Think\Controller,
    Think\Page,
    Admin\Model\AclModel;

class AclController extends Controller
{
    public $model_acl;

    public function _initialize()
    {
        $this->model_acl = new AclModel();
    }

    //节点列表
    public function node_list()
    {
        $node_pid = intval(I('get.node_pid'));
        if($node_pid < 1){ $node_pid = 0; }
        if($node_pid == 0){
            $list = $this->model_acl->getAclNode(2, array('node_pid'=>$node_pid));
        }else{
            $list = $this->model_acl->getAclNode(2, array('node_pid_path'=>','.$node_pid.','));
        }
        $list = list_to_tree($list, 'node_id', 'node_pid', '_sub', $node_pid);
        $this->assign('list', $list);
        $this->display();
    }

    //节点添加
    public function node_add()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->model_acl->addAclNode($post);
            if($rs['st'] == 'err'){ $this->error($rs['msg']); }
            $this->success('操作成功！', U('admin/acl/node_list'));
            exit;
        }

        $params['node_pid'] = I('get.node_pid');
        $params['nodel_level'] = I('get.nodel_level');
        $params['node_pid'] = isset($params['node_pid']) ? (int)$params['node_pid'] : 0;
        $params['nodel_level'] = isset($params['nodel_level']) ? (int)$params['nodel_level'] : 4;
        if($params['node_pid'] == 0){
            $this->assign('node_pid', $params['node_pid']);
            $this->assign('node_parents', '顶级菜单');
            $this->assign('nodel_level', 1);
        }else{
            $tmp = $this->model_acl->getAclNode(4, array('node_id'=>$params['node_pid']));
            $this->assign('node_pid', $params['node_pid']);
            $this->assign('node_parents', $tmp['node_name']);
        }
        $this->display();
    }

    //节点修改
    public function node_edit()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->model_acl->editAclNode($post);
            if($rs['st'] == 'err'){ $this->error($rs['msg']); }
            $this->success('操作成功！', U('admin/acl/node_list'));
            exit;
        }

        $node_id = intval(I('get.node_id'));
        if($node_id < 1){ $this->error('参数错误'); }
        $detail = $this->model_acl->getAclNode(4, array('node_id'=>$node_id));
        if($detail === false){ $this->error('未找到数据'); }
        //节点等级
        if($detail['node_pid'] == '0'){
            $this->assign('node_parents', '顶级节点');
        }else{
            $nm = $this->model_acl->getAclNode(4, array('node_id'=>$detail['node_pid']), 'node_name');
            if($nm === false){
                $this->assign('node_parents', '节点丢失');
            }else{
                $this->assign('node_parents', $nm['node_name']);
            }
        }
        $this->assign('detail', $detail);
        $this->display();
    }

    //删除节点
    public function node_del()
    {
        $node_id = intval(I('get.node_id')); if($node_id < 1){ $this->error('参数错误'); }
        $rs = $this->model_acl->delAclNode($node_id);
        if($rs['st'] == 'err'){
            $this->error($rs['msg']);
        }else{
            $this->success('删除成功', U('admin/acl/node_list'));
        }
    }

    //得到节点checkbox
    public function ajax_get_nodes_checkbox()
    {
        $group_id = abs((int)I('get.group_id'));
        echo $this->model_acl->getGroupNodesToCheckbox($group_id);
        exit;
    }

    //ajax改变节点状态
    public function ajax_node_st()
    {
        $st = intval(I('post.st')); if($st!=0 && $st!=1){ $st = 0; }
        $node_id = intval(I('post.node_id')); if($node_id < 1){ echo '参数错误！'; exit; }
        $rs = $this->model_acl->updateAclNodeByNodeId(array('st'=>$st), $node_id);
        if($rs['st'] == 'err'){
            echo $rs['msg'];
        }else{
            echo 'ok';
        }
        exit;
    }

    //分组列表
    public function group_list()
    {
        $m_group = M(getTable('tb_acl_group'));
        $list = $m_group->select();
        if($list){
            $list = list_to_tree($list, 'group_id', 'group_pid', '_sub');
            floatArray($list, '_sub', $rs);
        }
        $this->assign('list', $rs);
        $this->display();
    }

    //分组添加
    public function group_add()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->model_acl->addAclGroup($post);
            if($rs['st'] == 'err'){ $this->error($rs['msg']); }
            $this->success('操作成功！', U('admin/acl/group_list'));
            exit;
        }
        $nodes = $this->model_acl->getAclNode(2, array('status'=>1), 'node_id,node_pid,node_name');
        if(is_array($nodes) && count($nodes)){
            $nodes = list_to_tree($nodes, 'node_id', 'node_pid', '_sub');
        }
        $this->assign('nodes', $nodes);
        $this->assign('groups', $this->model_acl->getAclGroupSelect());
        $this->display();
    }

    //分组修改
    public function group_edit()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->model_acl->editAclGroup($post);
            if($rs['st'] == 'err'){ $this->error($rs['msg']); }
            $this->success('操作成功！', U('admin/acl/group_list'));
            exit;
        }

        $group_id = intval(I('get.group_id'));
        if($group_id < 0){ $this->error('参数错误'); }
        $detail = $this->model_acl->getAclGroup(4, array('group_id'=>$group_id));
        if($detail === false){ $this->error('参数错误'); }

        $group_nodes = explode(',', $detail['group_acl']);
        $nodes = $this->model_acl->getAclNode(2, array('status'=>1), 'node_id,node_pid,node_name');
        foreach ($nodes as $k=>$v){
            $nodes[$k]['has'] = in_array($v['node_id'], $group_nodes) ? 1 : 0;
        }
        $nodes = list_to_tree($nodes, 'node_id', 'node_pid', '_sub');
        $this->assign('nodes', $nodes);

        $this->assign('groups', $this->model_acl->getAclGroupSelect($detail['group_pid'], 'group_pid'));
        $this->assign('detail', $detail);
        $this->display();
    }

    //删除分组
    public function group_del()
    {
        $group_id = intval(I('get.group_id')); if($group_id < 1){ $this->error('参数错误'); }
        $rs = $this->model_acl->delAclGroup($group_id);
        if($rs['st'] == 'err'){
            $this->error($rs['msg']);
        }else{
            $this->success('删除成功', U('admin/acl/group_list'));
        }
    }

    //ajax更改group状态
    public function ajax_group_st()
    {
        $st = intval(I('post.st')); if($st!=0 && $st!=1){ $st = 0; }
        $group_id = intval(I('post.group_id')); if($group_id < 1){ echo '参数错误！'; exit; }
        $rs = $this->model_acl->updateAclGroupByGroupId(array('st'=>$st), $group_id);
        if($rs['st'] == 'err'){
            echo $rs['msg'];
        }else{
            echo 'ok';
        }
        exit;
    }

    //用户列表
    public function admin_list()
    {
        $param = I('get.');
        $page = intval(I('get.p')); if($page < 1){ $page = 1; }
        $pageSize = 20;
        $param['root'] = false;
        $list = $this->model_acl->getAclAdmin(3, $param, 'a.admin_id,a.admin_group_id,a.admin_name,a.st,b.group_name', $page, $pageSize);
        $pg = new Page($list['tot'], $pageSize);
        $this->assign('groups', $this->model_acl->getAclGroupSelect($param['admin_group_id'], 'admin_group_id'));
        $this->assign('list', $list['datas']);
        $this->assign('pager', $pg->show());
        $this->assign('param', $param);
        $this->display();
    }

    //添加用户
    public function admin_add()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->model_acl->addAclAdmin($post);
            if($rs['st'] == 'err'){ $this->error($rs['msg']); }
            $this->success('操作成功！', U('admin/acl/admin_list'));
            exit;
        }

        $this->assign('groups', $this->model_acl->getAclGroupSelect('0', 'admin_group_id', 'getGroupAcl(this.value)'));
        $this->display();
    }

    //修改用户
    public function admin_edit()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->model_acl->editAclAdmin($post);
            if($rs['st'] == 'err'){ $this->error($rs['msg']); }
            $this->success('操作成功！', U('admin/acl/admin_list'));
            exit;
        }

        $admin_id = intval(I('get.admin_id'));
        if($admin_id < 2){ $this->error('参数错误！'); } //不能修改root
        $detail = $this->model_acl->getAclAdmin(4, array('admin_id'=>$admin_id));
        $this->assign('detail', $detail);
        $this->assign('groups', $this->model_acl->getAclGroupSelect($detail['admin_group_id'], 'admin_group_id', 'getGroupAcl(this.value)', true));
        $this->assign('nodes', $this->model_acl->getGroupNodesToCheckbox(intval($detail['admin_group_id']), intval($detail['admin_id'])));
        $this->display();
    }

    //ajax更新用户状态
    public function ajax_admin_st()
    {
        $st = intval(I('post.st')); if($st!=0 && $st!=1){ $st = 0; }
        $admin_id = intval(I('post.admin_id')); if($admin_id < 2){ echo '参数错误！'; exit; }
        $rs = $this->model_acl->updateAclAdminByAdminId(array('st'=>$st), $admin_id);
        if($rs['st'] == 'err'){
            echo $rs['msg'];
        }else{
            echo 'ok';
        }
        exit;
    }

    //空方法
    public function _empty()
    {
        $this->redirect(U('admin/acl/node_list'));
    }
}